//
//  ShareController.swift
//  rxExample
//
//  Created by 刘山 on 2021/11/5.
//

import Foundation
import UIKit
import RxSwift
import RxCocoa
import SnapKit

@objc(ShareController)
public class ShareController: UIViewController {
    let disposeBag = DisposeBag()
    
    let usernameOutlet = UITextField()
    let usernameValidOutlet = UILabel()
    let doSomethingOutlet = UIButton()
    
    public override func viewDidLoad() {
        self.view.backgroundColor = UIColor.white
        layout()
        
        // 用户名是否有效
        let usernameValid = usernameOutlet.rx.text.orEmpty
            .map({ (text) -> Bool in
                print("Map:\(text)")
                return text.count >= 5
            })
            .share(replay: 1, scope: .forever)
        
        usernameValid.bind(to: usernameValidOutlet.rx.isHidden).disposed(by: disposeBag)
        usernameValid.bind(to: doSomethingOutlet.rx.isEnabled).disposed(by: disposeBag) // 第二次订阅
        //share_effects()
    }
    
    // MARK: - 共享附加作用
    func share_effects() {
        let obs = Observable<String>.create { observer in
            print("Request Started")
            observer.onNext("Request Completed")
            observer.onCompleted()
            return Disposables.create()
        }.share(replay: 1, scope: .forever)//.asDriver(onErrorJustReturn: "")
        
        obs.subscribe { print("First Subscribe:\($0)") }.disposed(by: disposeBag)
        obs.subscribe { print("Second Subscribe:\($0)") }.disposed(by: disposeBag) // 第二次订阅
    }
    
    func layout() {
        doSomethingOutlet.setTitle("Submit", for: .normal)
        doSomethingOutlet.setTitle("Can Not Submit", for: .disabled)
        doSomethingOutlet.setTitleColor(UIColor.white, for: .normal)
        doSomethingOutlet.setTitleColor(UIColor.lightGray, for: .disabled)
        doSomethingOutlet.backgroundColor = UIColor.blue
        
        self.view.addSubview(usernameOutlet)
        self.view.addSubview(usernameValidOutlet)
        self.view.addSubview(doSomethingOutlet)
        
        usernameOutlet.placeholder = "用户名："
        
        usernameOutlet.borderStyle = .roundedRect
        usernameOutlet.clearButtonMode = .whileEditing
        usernameOutlet.keyboardType = .emailAddress
        usernameOutlet.returnKeyType = .done
        
        usernameValidOutlet.text = "Username has to be at least \(5) characters"
        usernameValidOutlet.textColor = UIColor.red
        
        usernameOutlet.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(15)
            make.right.equalTo(self.view).offset(-15)
            make.top.equalTo(self.view).offset(100)
        }
        usernameValidOutlet.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(15)
            make.right.equalTo(self.view).offset(-15)
            make.top.equalTo(usernameOutlet.snp_bottom).offset(5)
        }
        
        doSomethingOutlet.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(15)
            make.right.equalTo(self.view).offset(-15)
            make.top.equalTo(usernameValidOutlet.snp_bottom).offset(10)
        }
    }
}
